package com.demo.apparel.service.uniapp.Impl;

import com.demo.apparel.dto.PlatformDTO;
import com.demo.apparel.dto.PlatformVisitDTO;
import com.demo.apparel.mapper.uniapp.PlatformVisitMapper;
import com.demo.apparel.service.uniapp.PlatformVisitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PlatformVisitServiceImpl implements PlatformVisitService {

    @Autowired
    private PlatformVisitMapper platformVisitMapper;

    @Override
    public void recordVisit(String uri, String platform) {
        platformVisitMapper.incrementTotalVisit(uri, platform);
        platformVisitMapper.incrementDailyVisit(uri, platform);
    }

    @Override
    public List<PlatformVisitDTO> getTodayVisit() {
        List<PlatformVisitDTO> dailyList = platformVisitMapper.selectTodayVisit();
        List<PlatformDTO> platformList = platformVisitMapper.selectTodayVisitByPlatform();
        for (PlatformVisitDTO daily : dailyList) {
            List<PlatformDTO> platforms = platformList.stream()
                    .filter(p -> p.getUri().equals(daily.getUri()))
                    .collect(Collectors.toList());
            daily.setPlatform(platforms);
        }

        return dailyList;
    }

    @Override
    public List<PlatformVisitDTO> getTotalVisit() {
        List<PlatformVisitDTO> totalList = platformVisitMapper.selectTotalVisit();
        List<PlatformDTO> platformList = platformVisitMapper.selectTotalVisitByPlatform();

        for (PlatformVisitDTO total : totalList) {
            List<PlatformDTO> platforms = platformList.stream()
                    .filter(p -> p.getUri().equals(total.getUri()))
                    .collect(Collectors.toList());
            total.setPlatform(platforms);
        }

        return totalList;
    }
}
